package com.imooc.course.${model}.controller.admin;

import com.imooc.course.${model}.form.${Domain}Form;
import com.imooc.course.${model}.form.PageForm;
import com.imooc.course.server.dto.${Domain}Dto;
import com.imooc.course.server.dto.PageDto;
import com.imooc.course.server.dto.ResponseDto;
import com.imooc.course.server.service.${Domain}Service;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/${domain}")
public class ${Domain}Controller {

    private static final Logger log = LoggerFactory.getLogger(${Domain}Controller.class);
    public static final String BUSINESS_NAME = "${tabNameCn}";


    @Resource(name = "${domain}Service")
    private ${Domain}Service ${domain}Service;

    /**
     * 查询列表
     * @param pageForm 传入的page和size
     * @return
     */
    @PostMapping("/list")
    public ResponseDto getList(@RequestBody PageForm pageForm) {
        PageDto pageDto = CopyUtil.copy(pageForm, PageDto.class);
        ${domain}Service.getList(pageDto);
        return ResponseDto.success().setContent(pageDto);
    }

    /**
     * 保存，id有值为更新，id无值为新增
     * @param ${domain}Form
     * @return
     */
    @PostMapping("/save")
    public ResponseDto add${Domain}(@RequestBody ${Domain}Form ${domain}Form) {
        //入参检查
        <#list fieldList as field>
            <#if !field.nullAble>
            <#if field.nameHump != 'id' && field.nameHump!="createdAt" && field.nameHump!="updatedAt">
        ValidatorUtil.notNull(${domain}Form.get${field.nameBigHump}(), "${field.nameCn}");
            </#if>
            </#if>
            <#if (field.length > 0)>
        ValidatorUtil.minimumToMaximum(${domain}Form.get${field.nameBigHump}(), "${field.nameCn}", 1, ${field.length?c});
            </#if>
        </#list>

        ${Domain}Dto ${domain}Dto = CopyUtil.copy(${domain}Form, ${Domain}Dto.class);
        ${domain}Service.save(${domain}Dto);
        return ResponseDto.success().setContent(${domain}Dto);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto del(@PathVariable String id) {
        ${domain}Service.delete(id);
        return ResponseDto.success();
    }
}
